﻿using System;
using Framework.MVC;
using Log.Model;
using Billing.Model;
using Billing.Model.Behavior;
using Framework.MVVM.Model;
using Common.IService;
using Log.IService;
using Billing.IService;
using Billing.Service;
using Log.Service;
using Common.Service;
using Billing.IService.Entry;
using Common.IService.Entry;
using Config.IService;
using Config.Service;
using Config.Model;
using Framework.Extention.Filter;


namespace Billing.Models
{
    [ServiceAttribute(typeof(IConfigService), typeof(ConfigService), "Instance")]
    [ServiceAttribute(typeof(IOrderService), typeof(PayOrderService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class QueryPagePayModel : AsyncModelBase, IWriteLog, IQueryOrderCount, IQueryOrderPage, IGetConfig<IFilter<string>>
    {
        #region Field

        private const string m_ip_error = "pagerpay0";
        private const string m_ip_complete = "pagerpay1";
        private const string m_count_error = "pagerpay2";
        private const string m_count_complete = "pagerpay3";
        private const string m_page_error = "pagerpay4";
        private const string m_page_complete = "pagerpay5";

        private const string m_count = "count";
        private const string m_data = "data";
        #endregion Field

        #region Property

        public string Source { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.GetConfig<IFilter<string>>(m_ip_error, m_ip_complete);
        }

        #endregion AsyncModelBase

        #region IWriteLog

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion IWriteLog


        #region IGetConfig IP Filter

        public string ConfigName { get; set; }

        public void OnGetConfigComplete(string key, IFilter<string> entry)
        {
            bool validate = entry.Validate(Source);
            if (validate)
            {
                OnBeginRequest();
                this.QueryOrderCount(m_count_error, m_count_complete);
            }
            this.Log(key, new SourceEntry<bool> { source = Source, data = validate });
        }

        public void OnGetConfigError(string key)
        {
            this.Log(key, Source);
        }

        #endregion IGetConfig IP Filter

        #region IQueryOrderCount

        public string OrderSourceCode { get; set; }

        public string OrderUser { get; set; }

        public string BeginTime { get; set; }

        public string EndTime { get; set; }

        public void OnQueryOrderCountComplete(string key, int entry)
        {
            OnComplete(m_count, entry);
            int top = entry - (PageIndex - 1) * PageSize;
            if (entry > 0 && top > 0)
            {
                PageTop = top >= PageSize ? PageSize : top;
                OnBeginRequest();
                this.QueryOrderPage(m_page_error,m_page_complete);
            }
            this.Log(key, new SourceEntry<int> { source = Source, data = entry });
        }

        public void OnQueryOrderCountError(string key)
        {
            this.Log(key, new SourceEntry<OrderQueryParam> { source = Source, data = new OrderQueryParam { user = OrderUser, sourceCode = OrderSourceCode, btime = BeginTime, etime = EndTime } });
        }

        #endregion IQueryOrderCount

        #region IQueryOrderPage

        public int PageTop { get; set; }

        public int PageIndex { get; set; }

        public int PageSize { get; set; }

        public void OnQueryOrderPageComplete(string key, OrderEntry[] entry)
        {
            OnComplete(m_data, entry);
            this.Log(key, new SourceEntry<OrderEntry[]> { source = Source, data = entry });
        }

        public void OnQueryOrderPageError(string key)
        {
            this.Log(key, new SourceEntry<OrderQueryParam> { source = Source, data = new OrderQueryParam { user = OrderUser, sourceCode = OrderSourceCode, btime = BeginTime, etime = EndTime } });
        }

        #endregion IQueryOrderPage
    }
}